package com.dykj.fireFighting.web.base.websocket.listener;

import com.dykj.fireFighting.common.core.utils.JacksonUtil;
import com.dykj.fireFighting.web.base.websocket.CommonWebSocket;
import com.dykj.fireFighting.web.base.websocket.entity.MoreMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

/**
 * @Author fgc
 * @Date 2021/7/30 15:52
 * @ Description：多人单点消息监听
 */
@Slf4j
@Component
public class MoreMessageListener implements MessageListener {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private CommonWebSocket commonWebSocket;


    @Override
    public void onMessage(Message message, byte[] bytes) {
        log.info("监听集群websocket消息--- message:{}", message);
        RedisSerializer<String> valueSerializer = redisTemplate.getStringSerializer();
        String value = valueSerializer.deserialize(message.getBody());
        if (StringUtils.isNotBlank(value)) {
            log.info("监听集群websocket消息--- value:{}", value);
            MoreMessage moreMessage = JacksonUtil.toObject(value, MoreMessage.class);
            System.out.println(moreMessage.getUserIds());
            log.info("监听集群websocket消息--- moreMessage:{}", moreMessage);

            //集群模式 推送消息
            commonWebSocket.sendMoreMessage(moreMessage.getUserIds(), value);
        }
    }
}
